<?php
/**
 * 新闻
 */

namespace app\admin\controller;


use app\common\controller\Controller;
use think\Request;
use think\Db;
use think\Exception;
use think\Loader;

use app\admin\model\News as NewsModel;

class News extends Common
{

    public function getIndex(Request $request)
    {
        
        $keyword=$request->param('keyword');
       
        $order_type=$request->param('order_type',1);

        $publish_status=$request->param('publish_status',-1);
        $query =NewsModel::getListQuery($keyword,$publish_status);

        if($order_type == 1)
        {
            $query->order('news_time desc');
        }else{
            $query->order('news_time');
        }
        $this->assign([
            'list'=>$query->paginate(),
            'keyword'=>$keyword,
            'order_type'=>$order_type,
            'publish_status'=>$publish_status
        ]);

        return $this->fetch('index');
    }

    public function postAjaxChangePublishStatus(Request $request)
    {
        $news_id =$request->post('id');
        $status = $request->post('status');
        if(!is_numeric($news_id))
        {
            die(json_encode(['status'=>false,'msg'=>'参数错误']));
        }
        if($status !=1 && $status !=0)
        {
            die(json_encode(['status'=>false,'msg'=>'未知状态']));
        }
        NewsModel::updatePublishStatus($news_id,$status);
        die(json_encode(['status'=>true,'msg'=>'']));
    }
    public function postAjaxDeleteNews(Request $request)
    {
        $news_id = $request->post('id');
        if(!is_numeric($news_id))
        {
            die(json_encode(false));
        }
        NewsModel::deleteNews($news_id);
        die(json_encode(true));
    }

    public function getEdit(Request $request)
    {
        $news_id = $request->param('news_id');

        if($news_id)
        {
            $info =NewsModel::handleInfo(NewsModel::getNewsInfo($news_id));
            $info['news_content']=htmlspecialchars(NewsModel::getNewsContent($news_id));
        }else{
            $info = ['news_title'=>'','news_content'=>'','news_time'=>''];
        }

        return $this->fetch('news/edit',['info'=>$info]);
    }
    public function postAjaxGetInfo(Request $request){
        $news_id = $request->post('news_id');

        if(empty($news_id)){
            die(json_encode([]));
        }
        $info =NewsModel::handleInfo(NewsModel::getNewsInfo($news_id));
        $info['news_content']=htmlspecialchars_decode(NewsModel::getNewsContent($news_id));
        die(json_encode($info));

    }
    public function postAjaxSaveDraft(Request $request)
    {
        //添加新闻时， 返回 ID， 防止 重复添加
        $news_id = $request->post('news_id');

        $content = $request->post('content');
        $title = $request->post('title');
        $save_time =$request->post('save_time');
//        $content='r仍无法ioo看人家人吉安人GVoooooo# &gro"eia惹我个人h"aw:eik';
//        $title='rger"eef让他人防r"o热风oke';

        if(empty($title))
        {
            die(json_encode(['status'=>false,'msg'=>'标题不能为空']));
        }

        if(empty($content))
        {
            die(json_encode(['status'=>false,'msg'=>'内容不能为空']));
        }

        $publish_time = strtotime($save_time);
        if(!$publish_time){
            $publish_time = time();
        }
        $data =[
            'news_time'=>$publish_time,
            'news_is_publish'=>0,
            'news_title'=>htmlspecialchars($title),

        ];

        Db::startTrans();
        try{

            if(!$news_id)
            {
                $news_id = Db::name('news_list')->insert($data,false,true);
                //保存 内容
                Db::name('news_content')->insert(['content_news_id'=>$news_id,'content_text'=>htmlspecialchars($content)]);
            }else{
                Db::name('news_list')->where(['news_id'=>$news_id])->update($data);
                //保存 内容
                Db::name('news_content')->where(['content_news_id'=>$news_id])->setField('content_text',htmlspecialchars($content));

            }

        }catch (Exception $e)
        {
             Db::rollback();
            die(json_encode(['status'=>false,'msg'=>'保存失败']));

        }

        Db::commit();
        //将 news_id 返回，用于下次保存草稿时 传入作为更新操作条件
        die(json_encode(['status'=>true,'msg'=>'','news_id'=>$news_id]));
    }
    public function postPublish(Request $request)
    {
//添加新闻时， 返回 ID， 防止 重复添加
        $news_id = $request->post('news_id');

        $content = $request->post('content');
        $title = $request->post('title');
        $save_time =$request->post('save_time');

        if(empty($title))
        {
            $this->error('标题不能为空');
        }

        if(empty($content))
        {
            $this->error('内容不能为空');
        }

        $publish_time = strtotime($save_time);
        if(!$publish_time){
            $publish_time = time();
        }
        $data =[
            'news_time'=>$publish_time,
            'news_is_publish'=>1,
            'news_title'=>htmlspecialchars($title)
        ];

        Db::startTrans();
        try{

            if(!$news_id)
            {
                $news_id = Db::name('news_list')->insert($data,false,true);
                //保存 内容
                Db::name('news_content')->insert(['content_news_id'=>$news_id,'content_text'=>htmlspecialchars($content)]);

            }else{
                Db::name('news_list')->where(['news_id'=>$news_id])->update($data);
                //保存 内容
                Db::name('news_content')->where(['content_news_id'=>$news_id])->setField('content_text',htmlspecialchars($content));

            }

        }catch (Exception $e)
        {
            Db::rollback();
            $this->error('保存失败');

        }

        Db::commit();

        $this->success('保存成功','/news/index');
    }

    //获取 编辑器配置
    public function  getAjaxUploadImg(Request $request){
        Loader::import('Ueditor.controller');

        $uploadHandler = new \UeditorUploadHandler();
        $action='config';
        die($uploadHandler->uploadFile($action,'news'));
//        $action = $request->get('action');
//
//        if($action == 'config'){
//          //  var_dump($uploadHandler->uploadFile($action,'news'));
//           die($uploadHandler->uploadFile($action,'news'));
//        }else if($action == 'uploadimage'){
//            $fileinfo= $uploadHandler->uploadFile($action,'news');
//            die(json_encode(['state'=>$fileinfo['state'],'url'=>'/'.$fileinfo['url']]));
//        }else{
//            die(json_encode(['state'=>'FAIL','msg'=>'未知Action']));
//        }
    }
    //上传图片
    public function  postAjaxUploadImg(Request $request){
        Loader::import('Ueditor.controller');

        $uploadHandler = new \UeditorUploadHandler();


        $action = $request->get('action');

        if($action == 'config'){
            //  var_dump($uploadHandler->uploadFile($action,'news'));
            die($uploadHandler->uploadFile($action,'news'));
        }else if($action == 'uploadimage'){
            $fileinfo= $uploadHandler->uploadFile($action,'news');
            die(json_encode(['state'=>$fileinfo['state'],'url'=>'/'.$fileinfo['url']]));
        }else{
            die(json_encode(['state'=>'图片上传失败']));
        }
    }
}